﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using DBUtility;

namespace OracleDAL.SUBSYSTEM
{
// <summary>
	/// 数据访问类SUBSYS_LOGIN。
	/// </summary>
	public class SUBSYS_LOGIN
	{
		public SUBSYS_LOGIN()
		{}
		#region  成员方法

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string SUBSYSTEMID,string USERISN)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from SUBSYS_LOGIN");
			strSql.Append(" where SUBSYSTEMID=:SUBSYSTEMID and USERISN=:USERISN ");
			OracleParameter[] parameters = {
					new OracleParameter(":SUBSYSTEMID", OracleType.VarChar,50),
					new OracleParameter(":USERISN", OracleType.VarChar,50)};
			parameters[0].Value = SUBSYSTEMID;
			parameters[1].Value = USERISN;

			return OracleHelper.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void Add(Model.SUBSYSTEM.SUBSYS_LOGIN model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into SUBSYS_LOGIN(");
			strSql.Append("SUBSYSTEMID,USERISN,USERNAME,USERPWD,REMARK,CREATETIME,MODIFYTIME)");
			strSql.Append(" values (");
			strSql.Append(":SUBSYSTEMID,:USERISN,:USERNAME,:USERPWD,:REMARK,:CREATETIME,:MODIFYTIME)");
			OracleParameter[] parameters = {
					new OracleParameter(":SUBSYSTEMID", OracleType.VarChar,25),
					new OracleParameter(":USERISN", OracleType.VarChar,25),
					new OracleParameter(":USERNAME", OracleType.VarChar,50),
					new OracleParameter(":USERPWD", OracleType.VarChar,50),
					new OracleParameter(":REMARK", OracleType.VarChar,1000),
					new OracleParameter(":CREATETIME", OracleType.DateTime),
					new OracleParameter(":MODIFYTIME", OracleType.DateTime)};
			parameters[0].Value = model.SUBSYSTEMID;
			parameters[1].Value = model.USERISN;
			parameters[2].Value = model.USERNAME;
			parameters[3].Value = model.USERPWD;
			parameters[4].Value = model.REMARK;
			parameters[5].Value = model.CREATETIME;
			parameters[6].Value = model.MODIFYTIME;
            if (Exists(model.SUBSYSTEMID, model.USERISN)) {
                Delete(model.SUBSYSTEMID, model.USERISN);
            }
			OracleHelper.ExecuteSql(strSql.ToString(),parameters);
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public void Update(Model.SUBSYSTEM.SUBSYS_LOGIN model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update SUBSYS_LOGIN set ");
			strSql.Append("USERNAME=:USERNAME,");
			strSql.Append("USERPWD=:USERPWD,");
			strSql.Append("REMARK=:REMARK,");
			strSql.Append("CREATETIME=:CREATETIME,");
			strSql.Append("MODIFYTIME=:MODIFYTIME");
			strSql.Append(" where SUBSYSTEMID=:SUBSYSTEMID and USERISN=:USERISN ");
			OracleParameter[] parameters = {
					new OracleParameter(":SUBSYSTEMID", OracleType.VarChar,25),
					new OracleParameter(":USERISN", OracleType.VarChar,25),
					new OracleParameter(":USERNAME", OracleType.VarChar,50),
					new OracleParameter(":USERPWD", OracleType.VarChar,50),
					new OracleParameter(":REMARK", OracleType.VarChar,1000),
					new OracleParameter(":CREATETIME", OracleType.DateTime),
					new OracleParameter(":MODIFYTIME", OracleType.DateTime)};
			parameters[0].Value = model.SUBSYSTEMID;
			parameters[1].Value = model.USERISN;
			parameters[2].Value = model.USERNAME;
			parameters[3].Value = model.USERPWD;
			parameters[4].Value = model.REMARK;
			parameters[5].Value = model.CREATETIME;
			parameters[6].Value = model.MODIFYTIME;

			OracleHelper.ExecuteSql(strSql.ToString(),parameters);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public void Delete(string SUBSYSTEMID,string USERISN)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from SUBSYS_LOGIN ");
			strSql.Append(" where SUBSYSTEMID=:SUBSYSTEMID and USERISN=:USERISN ");
			OracleParameter[] parameters = {
					new OracleParameter(":SUBSYSTEMID", OracleType.VarChar,50),
					new OracleParameter(":USERISN", OracleType.VarChar,50)};
			parameters[0].Value = SUBSYSTEMID;
			parameters[1].Value = USERISN;

			OracleHelper.ExecuteSql(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Model.SUBSYSTEM.SUBSYS_LOGIN GetModel(string SUBSYSTEMID,string USERISN)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select SUBSYSTEMID,USERISN,USERNAME,USERPWD,REMARK,CREATETIME,MODIFYTIME from SUBSYS_LOGIN ");
			strSql.Append(" where SUBSYSTEMID=:SUBSYSTEMID and USERISN=:USERISN ");
			OracleParameter[] parameters = {
					new OracleParameter(":SUBSYSTEMID", OracleType.VarChar,50),
					new OracleParameter(":USERISN", OracleType.VarChar,50)};
			parameters[0].Value = SUBSYSTEMID;
			parameters[1].Value = USERISN;

			Model.SUBSYSTEM.SUBSYS_LOGIN model=new Model.SUBSYSTEM.SUBSYS_LOGIN();
			DataSet ds=OracleHelper.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				model.SUBSYSTEMID=ds.Tables[0].Rows[0]["SUBSYSTEMID"].ToString();
				model.USERISN=ds.Tables[0].Rows[0]["USERISN"].ToString();
				model.USERNAME=ds.Tables[0].Rows[0]["USERNAME"].ToString();
				model.USERPWD=ds.Tables[0].Rows[0]["USERPWD"].ToString();
				model.REMARK=ds.Tables[0].Rows[0]["REMARK"].ToString();
				if(ds.Tables[0].Rows[0]["CREATETIME"].ToString()!="")
				{
					model.CREATETIME=DateTime.Parse(ds.Tables[0].Rows[0]["CREATETIME"].ToString());
				}
				if(ds.Tables[0].Rows[0]["MODIFYTIME"].ToString()!="")
				{
					model.MODIFYTIME=DateTime.Parse(ds.Tables[0].Rows[0]["MODIFYTIME"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select SUBSYSTEMID,USERISN,USERNAME,USERPWD,REMARK,CREATETIME,MODIFYTIME ");
			strSql.Append(" FROM SUBSYS_LOGIN ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return OracleHelper.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			OracleParameter[] parameters = {
					new OracleParameter(":tblName", OracleType.VarChar, 255),
					new OracleParameter(":fldName", OracleType.VarChar, 255),
					new OracleParameter(":PageSize", OracleType.Number),
					new OracleParameter(":PageIndex", OracleType.Number),
					new OracleParameter(":IsReCount", OracleType.Clob),
					new OracleParameter(":OrderType", OracleType.Clob),
					new OracleParameter(":strWhere", OracleType.VarChar,1000),
					};
			parameters[0].Value = "SUBSYS_LOGIN";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return OracleHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  成员方法
	}
}

